package com.yc.booktable.controller;

import com.yc.booktable.entity.AdminUser;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpSession;

/**
 * @program: phase-iii-project
 * @description:
 * @author: 作者 zl
 * @create: 2022-06-07 15:17
 */
@Controller
public class IndexController {

    @GetMapping("login")
    public String toLogin() {
        return "login";
    }

    @GetMapping("register")
    public String toRegister(){
        return "register";
    }

    @GetMapping("specials")
    public String specials(Model model,HttpSession session){
        AdminUser user = (AdminUser) session.getAttribute("loginedUser");
        model.addAttribute("user",user);
        return "specials";
    }
    @GetMapping("edituser")
    public String edituser(){
        return "edituser";
    }


    @GetMapping(value = {"/","index"})
    public String index(HttpSession session,Model model){
        AdminUser loginedUser = (AdminUser) session.getAttribute("loginedUser");
        model.addAttribute("user",loginedUser);
        return "index";
    }

    @GetMapping("book-a-table")
    public String BookaTable(HttpSession session, Model model) {
        AdminUser user = (AdminUser) session.getAttribute("loginedUser");
        if(user==null){
            return "redirect:login";
        }
        model.addAttribute("user",user);
        return "book-a-table";
    }

    @GetMapping("gallery")
    public String gallery(HttpSession session,Model model){
        AdminUser user = (AdminUser) session.getAttribute("loginedUser");
        model.addAttribute("user",user);
        return "gallery";
    }

    @GetMapping("chefs")
    public String chefs(HttpSession session,Model model){
        AdminUser user = (AdminUser) session.getAttribute("loginedUser");
        model.addAttribute("user",user);
        return "chefs";
    }

    @GetMapping("about")
    public String about(HttpSession session,Model model){
        AdminUser user = (AdminUser) session.getAttribute("loginedUser");
        model.addAttribute("user",user);
        return "about";
    }

    @RequestMapping("contact")
    public String contact(Model model, HttpSession session){
        AdminUser user = (AdminUser) session.getAttribute("loginedUser");
        if(user==null){
            return "redirect:login";
        }
        model.addAttribute("user",user);
        return "contact";
    }

    @GetMapping("logout")
    public String logout(HttpSession session,Model model){
        session.invalidate();
        model.addAttribute("user",null);
        return "index";
    }

    @GetMapping("personal")
    public String personal(HttpSession session,Model model){
        AdminUser user = (AdminUser) session.getAttribute("loginedUser");
        if(user==null){
            return "redirect:login";
        }
        model.addAttribute("user",user);
        return "personal";
    }

    @GetMapping("feedback")
    public String feedback(HttpSession session,Model model){
        AdminUser user = (AdminUser) session.getAttribute("loginedUser");
        if(user==null){
            return "redirect:login";
        }
        model.addAttribute("user",user);
        return "feedback";
    }

    @GetMapping("bookinfo")
    public String bookinfo(HttpSession session,Model model){
        AdminUser user = (AdminUser) session.getAttribute("loginedUser");
        if(user==null){
            return "redirect:login";
        }
        model.addAttribute("user",user);
        return "bookinfo";
    }


}
